<?php
namespace Admin\Controller;

class TraderController extends \Admin\Controller\AdminController
{
	/**
	 * 基金经理列表
	 * @author 刘新
	 */
	public function traderList()
	{
		$con = I('condition');
		$where['status'] = array('egt', 0);
		
		//查询条件
		if($con) {
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			} else {
				$where['name'] = array('like', '%'.$con.'%');
			}
		}
		
		//获取基金经理信息
		$list = $this->lists('Trader', $where);
		
		if(!empty($list)) {
			int_to_string($list);
			
			foreach($list as &$val) {
				$val['education_text'] = C('EDUCATION')[$val['education']];
			}
		}
		
		//die(json_encode($list));
		$this->assign('_title', '基金经理列表');
		$this->assign('_list', $list);
		$this->display('list');
	}
	
	/**
	 * 添加基金经理
	 * @author 刘新
	 */
	public function addTrader()
	{
		if(IS_GET) {
			$this->assign('_title', '新增基金经理');
			$this->display('add_edit');
		}
	
		//添加基金经理
		$res = D('Trader')->addTrader();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->success('添加成功', U('Trader/traderList'));
	}
	
	/**
	 * 编辑基金经理
	 * @author 刘新
	 */
	public function editTrader()
	{
		$id = I('id', '', '/^[1-9]\d*$/');
		!$id && $this->error('请选择要编辑的基金经理');
		
		if(IS_GET) {
			$trader = M('Trader')->where(array('id' => $id))->find();
			!$trader && $this->error('基金经理信息查询失败');
			$this->assign('trader', $trader);
			$this->assign('_title', '编辑基金经理');
			$this->display('add_edit');
		}
	
		//添加基金经理
		$res = D('Trader')->editTrader();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->success('编辑成功', U('Trader/traderList'));
	}
	
	/**
	 * 基金经理详情
	 * @author 刘新
	 */
	public function traderDetail()
	{
		$id = I('id', '', '/^[1-9]\d*$/');
		!$id && $this->error('参数错误');
	
		//查询基金经理信息
		//$trader = M('Trader')->where(array('id' => $id))->find();
		$trader = $this->returnOne('Trader', array('id' => $id));
		if(!$trader)  $this->error('基金经理信息查询失败');
		
		//查询头像
		$avatar = $this->returnOne('File', array('id' => $trader['avatar_id']), '', 'id,name,url');
		if($avatar) $trader['avatar_url'] = $avatar['url'];
		
		$trader['education_text'] = C('EDUCATION')[$trader['education']];
		$trader['sex_text'] = C('SEX')[$trader['sex']];
		
		//查询基金产品信息
		$fund = $this->returnList('Fund', array('trader_id' => $trader['id'], 'status' => array('egt', '-1')), 'praise_count', 'id,status,fund_name,create_time,comment_count,praise_count');
		if($fund === false) $this->error('基金产品信息查询失败');
		$fund = array();
		int_to_string($fund);
		
		foreach ($fund as $key => &$val) {
			if(!$val['comment_count']) {
				$val['comment_count'] = 0;
				$val['praise_rate'] = 100;
			} else {
				$val['praise_rate'] = bcdiv($val['comment_count'], $val['praise_count'], 4) * 100;
			}
		}
		
		$trader = array($trader);
		int_to_string($trader);
		$this->assign('trader', $trader[0]);
		$this->assign('_title', '基金经理详细信息');
		$this->assign('fund', $fund);
		$this->display('detail');
	}
	
	/**
	 * 更新基金经理状态
	 * @author 刘新
	 */
	public function changeStatus($method=null)
	{
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) $this->error('请选择要操作的数据!');
		$map['uid'] =   array('in',$id);
	
		switch ( strtolower($method) )
		{
			case 'forbid':
				$this->forbid('Trader', $map );
				break;
			case 'resume':
				$this->resume('Trader', $map );
				break;
			case 'delete':
				$this->delete('Trader', $map );
				break;
			default:
				$this->error('参数非法');
		}
	}
}